package com.aboo.multithread;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.collections4.ListUtils;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author louan
 * @date 2025-01-13
 * @description
 */
public class ThreadTest01 {

    @Test
    public void test01() {
        CompletableFuture.supplyAsync(() -> "world")
                .thenApplyAsync(u -> "Hello " + u)
                .thenAcceptAsync(System.out::println);
    }

    @Test
    public void test001() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        int partitionSize = 3;

        List<List<Integer>> partitions = ListUtils.partition(list, partitionSize);

        for (List<Integer> partition : partitions) {
            System.out.println(partition);
        }
    }

    @Test
    public void test02() {
        System.out.println("开始:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
        // 创建三个异步任务
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            // 模拟耗时操作
            sleep(1000);
            return "Task 1";
        });

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            // 模拟耗时操作
            sleep(2000);
            return "Task 2";
        });

        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            // 模拟耗时操作
            sleep(3000);
            return "Task 3";
        });

        // 等待所有任务完成
        CompletableFuture<Void> allOf = CompletableFuture.allOf(future1, future2, future3);

        // 获取所有任务的结果
        allOf.thenRun(() -> {
            try {
                System.out.println(future1.get());
                System.out.println(future2.get());
                System.out.println(future3.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }).join();
        System.out.println("结束:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
    }

    @Test
    public void test03() {
        System.out.println("开始:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
        // 创建三个异步任务
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            // 模拟耗时操作
            sleep(1000);
            return "Task 1";
        });

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            // 模拟耗时操作
            sleep(2000);
            return "Task 2";
        });

        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            // 模拟耗时操作
            sleep(3000);
            return "Task 3";
        });

        CompletableFuture<String>[] futureArr = new CompletableFuture[]{future1, future2, future3};

        // 等待所有任务完成
        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureArr);

        // 获取所有任务的结果
        allOf.thenRun(() -> {
            for (CompletableFuture<String> future : futureArr) {
                try {
                    System.out.println(future.get());
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).join();
        System.out.println("结束:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
    }

    private static void sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
